﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class giaovien_TrangDanhSachPhuHuynh : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                string magv = Session["Username"].ToString();
                cls.LoadComboboxNamHoc(drNamHoc);
                cls.LoadComboboxLopHocTheoMaGV(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()),magv);
                int manam = int.Parse(drNamHoc.SelectedValue.ToString());
                int malop = int.Parse(drLopHoc.SelectedValue.ToString());
                LoadGridPhuHuynh(manam, malop);
                LayThongTinPhuHuynh(manam, malop);
            }
        }
    }
    private void LayThongTinPhuHuynh(int manam, int malop)
    {
        
        grvPhuHuynh.DataSource = db.funcLayThongTinPhuHuynh(manam, malop);
        grvPhuHuynh.DataBind();
    }
    private void LoadGridPhuHuynh(int manam, int malop)
    {
        var c = from p in db.Lop_HocSinhs
                where p.MaNam == manam && p.MaLop == malop
                select new { p.MaHS, p.HocSinh.TenHS, p.HocSinh.HoTenCha, p.HocSinh.HoTenMe, p.HocSinh.DiaChiCha, p.HocSinh.DiaChiMe, p.HocSinh.Email, p.HocSinh.SDT };
        grvDSPH.DataSource = c;
        grvDSPH.DataBind();
    }
    private void CapNhatMK()
    {
        for (int i = 0; i < grvPhuHuynh.Rows.Count; i++)
        {
            Label lblMaHS = grvPhuHuynh.Rows[i].FindControl("lblMaHS") as Label;
            TextBox txtMK = grvPhuHuynh.Rows[i].FindControl("txtMatKhauPH") as TextBox;
            db.procCapNhatMatKhau(lblMaHS.Text, txtMK.Text);
        }
    }
    protected void grvPhuHuynh_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int hangchon = e.RowIndex;
        TextBox txtMK = grvPhuHuynh.Rows[hangchon].FindControl("txtMatKhauPH") as TextBox;
        Label lblMaHS = grvPhuHuynh.Rows[hangchon].FindControl("lblMaHS") as Label;
        txtMK.Text = "";
        db.procCapNhatMatKhau(lblMaHS.Text, txtMK.Text);
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        LayThongTinPhuHuynh(manam,malop);
    }
}